package com.xinqi.modules.sys.dict.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.modules.sys.dict.domain.DictVersionStageEntity;
import com.xinqi.modules.sys.dict.dto.req.DictVersionStageQueryDTO;
import com.xinqi.modules.sys.dict.dto.rsp.DictStageResultDTO;
import com.xinqi.modules.sys.dict.dto.rsp.DictVersionStageResultDTO;
import com.xinqi.modules.sys.dict.manager.DictVersionStageManager;
import com.xinqi.modules.sys.dict.mapper.DictVersionStageMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 字典-版本学科管理层实现
 *
 * @author: linkai
 * @date: 2023/05/07
 */
@Service
@RequiredArgsConstructor
public class DictVersionStageManagerImpl extends BaseServiceImpl<DictVersionStageMapper, DictVersionStageEntity> implements DictVersionStageManager {

    /**
     * 自定义分页查询
     */
    @Override
    public Page<DictVersionStageResultDTO> findPage(Page<Object> page, DictVersionStageQueryDTO query) {
        return this.baseMapper.findPage(page, query);
    }

    /**
     * 自定义列表查询
     */
    @Override
    public List<DictVersionStageResultDTO> findList(DictVersionStageQueryDTO query) {
        return this.baseMapper.findList(query);
    }

    /**
     * 根据id查询 字典-版本学科数据
     */
    @Override
    public DictVersionStageEntity findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return getById(id);
    }

    /**
     * 根据id删除 字典-版本学科数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return this.removeById(id);
    }

    @Override
    public List<DictStageResultDTO> findStageByVersionId(Long versionId) {
        return this.baseMapper.findStageByVersionId(versionId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addStage(Long versionId, List<Long> stageIdList) {
        // 删除之前
        delStage(versionId);
        // 再添加
        if (CollectionUtils.isEmpty(stageIdList)) {
            return true;
        }
        List<DictVersionStageEntity> list = stageIdList.stream().map(v -> {
            DictVersionStageEntity entity = new DictVersionStageEntity();
            entity.setVersionId(versionId);
            entity.setStageId(v);
            return entity;
        }).collect(Collectors.toList());
        return this.saveBatch(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delStage(Long versionId) {
        LambdaQueryWrapper<DictVersionStageEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DictVersionStageEntity::getVersionId, versionId);
        return this.baseMapper.delete(wrapper) > 0;
    }
}
